package com.tqjc.system.C24012.core.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tqjc.system.C24012.common.constant.ConfigConstants;
import com.tqjc.system.C24012.common.entity.BO.ConfigInfoBO;
import com.tqjc.system.C24012.common.entity.SysConfigEntity;
import com.tqjc.system.C24012.common.enums.ConfigCacheEnum;
import com.tqjc.system.C24012.core.service.ConfigService;
import com.tqjc.system.C24012.core.wrapper.ConfigInfoWrapper;
import com.tqjc.system.common.controller.BaseController;
import com.tqjc.system.core.model.AjaxJson;
import com.tqjc.system.core.model.PageInfo;
import com.tqjc.system.core.util.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

/**
 * @Author: 86195
 * @CreateTime: 2024-06-27  17:06
 * @Description: TODO
 * @Version: 1.0
 */
@Slf4j
@Controller
@RequestMapping("sys/config")
public class ConfigController extends BaseController {


    @Autowired
    private ConfigService configService;


    /**
     * 获取库区实际间隔
     * @return
     */
    @ResponseBody
    @RequestMapping("/getAreaInterval")
    public AjaxJson initAreaInterval(){
        AjaxJson ajaxJson = new AjaxJson();
        SysConfigEntity configEntity = configService.getConfigById(ConfigConstants.CONFIG_ID_AREA_INTERVAL);
        ajaxJson.setSuccess(true);
        ajaxJson.setObj(configEntity.getConfigValue());
        return ajaxJson;
    }


    @RequestMapping("/index")
    public String toConfigIndex(){
        return "C24012/config/index.html";

    }

    @RequestMapping("/query")
    @ResponseBody
    public void index(ConfigInfoBO configInfoBO, PageInfo<SysConfigEntity> page, HttpServletResponse response) {
        log.info("配置信息查询首页接口开始");
        try {

            IPage<SysConfigEntity> list = configService.index(page.getPageEntity(),configInfoBO);
            ResponseUtil.write(response, new ConfigInfoWrapper(list).wrap());
        } catch (Exception e){
            log.error("配置查询页面出现异常",e);
        }
        log.info("配置信息查询首页接口结束");

    }



    @RequestMapping("/query/id")
    public String getDetail(String id, Model model){
        log.info("配置详情查询接口开始");
        try {
            SysConfigEntity sysConfigEntity = configService.getById(id);
            model.addAttribute("msgDetail",sysConfigEntity);
        } catch (Exception e){
            log.error("配置详情查询接口出现异常",e);

        }
        log.info("配置查询接口结束");
        return "C24012/config/configDetail.html";
    }

    @RequestMapping("/modify")
    @ResponseBody
    public AjaxJson modify(ConfigInfoBO configInfoBO){
        AjaxJson j = new AjaxJson();
        log.info("配置信息更新接口开始");
        try {
            SysConfigEntity sysConfigEntity = SysConfigEntity.builder().id(Long.valueOf(configInfoBO.getId()))
                    .configValue(configInfoBO.getConfigValue())
                    .configStatus(configInfoBO.getConfigStatus()).build();
            configService.updateById(sysConfigEntity);
            //todo 如有缓存配置再加上此内容
//            ConfigCacheEnum configCacheEnum = ConfigCacheEnum.getConfigCacheEnum(configInfoBO.getConfigId(), configInfoBO.getConfigType());
//            if(configCacheEnum != null){
//                Object value;
//                String configValue = configInfoBO.getConfigValue();
//                if(ConfigConstant.CONFIG_STATUS_UNEFFECTIVE.equals(configInfoBO.getConfigStatus())){
//                    value = false;
//                } else {
//                    value = Boolean.valueOf(configValue);
//                }
//                configCacheEnum.refreshCache(value);
//            }

        } catch (Exception e){
            log.error("配置信息更新出现异常",e);
            j.setSuccess(false);
            j.setMsg("配置信息更新出现异常");
            return j;
        }
        j.setSuccess(true);
        j.setMsg("操作成功");
        log.info("配置信息更新接口结束");
        return j;
    }




}
